package com.pd.backend.service.logistics.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pd.backend.common.PageResult;
import com.pd.backend.common.R;
import com.pd.backend.common.utils.StringUtils;
import com.pd.backend.common.utils.id.IdUtils;
import com.pd.backend.common.utils.page.PageUtils;
import com.pd.backend.domain.entity.PdTruckTypeGoodsType;
import com.pd.backend.domain.pojo.GoodsType;
import com.pd.backend.domain.pojo.TruckType;
import com.pd.backend.ex.CustomerException;
import com.pd.backend.mapper.TruckGoodsMapper;
import com.pd.backend.mapper.TruckTypeMapper;
import com.pd.backend.service.logistics.TruckTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 车辆业务层实现类
 */
@Service
@Slf4j
public class TruckTypeServiceImpl implements TruckTypeService {

    @Autowired
    private TruckTypeMapper truckTypeMapper;

    @Autowired
    private TruckGoodsMapper truckGoodsMapper;

    /**
     * 多条件分页查询
     *
     * @param size            每页数据条数
     * @param current         当前页
     * @param allowableVolume 车辆载量
     * @param allowableLoad   车辆体积
     * @param name            车辆类型名称
     * @return
     */
    @Override
    public PageResult<TruckType> getPage(Integer size, Integer current, String allowableVolume, String allowableLoad, String name) {
        // 处理参数
        if (size == null || size <= 0) {
            size = 10;
        }
        if (current == null || current <= 0) {
            current = 1;
        }
        if (StringUtils.isNotEmpty(allowableVolume)) {
            allowableVolume = "%" + allowableVolume + "%";
        }
        if (StringUtils.isNotEmpty(allowableLoad)) {
            allowableLoad = "%" + allowableLoad + "%";
        }
        if (StringUtils.isNotEmpty(name)) {
            name = "%" + name + "%";
        }

        // 开始分页
        Page<TruckType> page = PageHelper.startPage(current, size);
        // 查询数据库
        List<TruckType> list = truckTypeMapper.getSearch(allowableVolume, allowableLoad, name);
        if (list.size() < 0) {
            throw new CustomerException("数据库查询失败");
        }

        return PageUtils.copyPage(page);
    }

    /**
     * 新增车辆类型
     *
     * @param truckType
     * @return
     */
    @Override
    @Transactional
    // TODO  id 不是分布式id
    public R save(TruckType truckType) {
        if (Objects.isNull(truckType)) {
            throw new CustomerException("参数异常");
        }
        // 商品类型
        List<GoodsType> goodsTypes = truckType.getGoodsTypes();
        if (Objects.isNull(goodsTypes) || goodsTypes.size() <= 0) {
            throw new CustomerException("商品类型异常");
        }
        // 保存车辆类型
        int i = truckTypeMapper.save(truckType);
        // 只需要将 商品id和车辆id添加到中间表
        for (GoodsType goodsType : goodsTypes) {
            // id保存到中间表
            PdTruckTypeGoodsType type = new PdTruckTypeGoodsType();
            type.setTruckTypeId(truckType.getId());
            type.setGoodsTypeId(goodsType.getId());
            int x = truckGoodsMapper.saveTruckGoods(type);
            // 测试事务
//            int aa = 1/0;
            if (x <= 0) {
                throw new CustomerException("中间表保存失败");
            }
        }
        if (i <= 0) {
            throw new CustomerException("保存失败");
        }
        R result = new R();
        result.setCode(200);
        result.setMsg("保存成功");
        return result;
    }

    /**
     * 修改车辆类型
     *
     * @param truckType
     * @return
     */
    @Override
    @Transactional
    public R modify(TruckType truckType) {
        // 处理参数
        if (Objects.isNull(truckType)) {
            throw new CustomerException("参数异常");
        }
        List<GoodsType> goodsTypes = truckType.getGoodsTypes();
        // 修改车辆类型信息
        int i = truckTypeMapper.modify(truckType);
        // 根据车辆类型id 将中间表的 的数据删除 再将商品id 和车类id添加进去
        int j = truckGoodsMapper.removeTruckGoodsByTruckId(truckType.getId());
        // 添加
        for (GoodsType goodsType : goodsTypes) {
            PdTruckTypeGoodsType type = new PdTruckTypeGoodsType();
            type.setGoodsTypeId(goodsType.getId());
            type.setTruckTypeId(truckType.getId());
            int x = truckGoodsMapper.saveTruckGoods(type);
        }
        R result = new R();
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }

    /**
     * 删除车辆类型
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public R remove(String id) {
        // 处理参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomerException("参数异常");
        }
        // 删除中间表数据
        // 根据车辆类型id 将中间表的 的数据删除
        int j = truckGoodsMapper.removeTruckGoodsByTruckId(id);
        // 删除车辆信息
        int i = truckTypeMapper.remove(id);
        R result = new R();
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }
}
